package com.totoro.tyrank.command;

import com.totoro.tyrank.TyRank;
import com.totoro.tyrank.constants.CommandConst;
import com.totoro.tyrank.constants.ReloadTypeConst;
import com.totoro.tyrank.enums.CommandEnum;
import com.totoro.tyrank.executor.command.MainCommandExecutor;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class RankCommand implements CommandExecutor, TabExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length == 0) {
            CommandEnum.HELP_COMMAND.getMainCommandExecutor().execute(sender, args);
            return false;
        }
        Bukkit.getScheduler().runTaskAsynchronously(TyRank.getInstance(), () -> {
            CommandEnum commandEnum = CommandEnum.getValue(args[0]);
            if(null == commandEnum) {
                sender.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                        TyRank.getLanguageManager().getLanguageData().getMsg().getCommandInputError());
                return;
            }
            MainCommandExecutor commandExecutor = commandEnum.getMainCommandExecutor();
            commandExecutor.execute(sender, args);
        });
        return false;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        if(args.length == 1) {
            if(args[0].startsWith("r")) {
                if(sender.isOp()) {
                    return Stream.of(CommandConst.RELOAD).collect(Collectors.toList());
                }
            }
            if(args[0].startsWith("a")) {
                return Stream.of(CommandConst.ALL).collect(Collectors.toList());
            }
            if(args[0].startsWith("o")) {
                return Stream.of(CommandConst.OPEN).collect(Collectors.toList());
            }
            if(args[0].startsWith("c")) {
                return Stream.of(CommandConst.CURRENT).collect(Collectors.toList());
            }
            if(args[0].startsWith("h")) {
                if(args[0].startsWith("hi")) {
                    return Stream.of(CommandConst.HISTORY).collect(Collectors.toList());
                }
                if(args[0].startsWith("he")) {
                    return Stream.of(CommandConst.HELP).collect(Collectors.toList());
                }
                return Stream.of(CommandConst.HISTORY, CommandConst.HELP).collect(Collectors.toList());
            }

            List<String> allCommand = Stream.of(CommandConst.OPEN, CommandConst.ALL, CommandConst.CURRENT, CommandConst.HISTORY, CommandConst.HELP).collect(Collectors.toList());
            if(sender.isOp()) {
                allCommand.add(CommandConst.RELOAD);
            }
            return allCommand;
        }

        if(args.length == 2) {
            if(args[0].equalsIgnoreCase("reload")) {
                if(sender.isOp()) {
                    // 获取所有排行榜Id
                    Set<String> rankIdSet = TyRank.getRankingManager().getRankDataMap().keySet();
                    List<String> reloadList = new ArrayList<>(rankIdSet);
                    reloadList.add(ReloadTypeConst.LANGUAGE);
                    reloadList.add(ReloadTypeConst.CONFIG);
                    return reloadList;
                }
            }
            if(args[0].equalsIgnoreCase("open")) {
                return new ArrayList<>(TyRank.getRankingManager().getRankDataMap().keySet());
            }
        }

        if(args.length == 3) {
            if(args[0].equalsIgnoreCase("open")) {
                if(TyRank.getRankingManager().getRankDataMap().containsKey(args[1])) {
                    if (args[2].startsWith("r")) {
                        return Stream.of("reward").collect(Collectors.toList());
                    }
                    if (args[2].startsWith("a")) {
                        return Stream.of("attribute").collect(Collectors.toList());
                    }
                    return Stream.of("reward", "attribute").collect(Collectors.toList());
                }
            }
        }
        return null;
    }
}
